<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        *{
            padding: 0;
            margin: 0;
        }
        .header{
            width: 300px;
            height: 300px;
            position: relative;
            margin: 330px auto;
        }
        .active{
            width: 40px;
            height: 40px;
            border: 1px solid rgb(150, 24, 24);
            font-size: 20px;
            line-height: 40px;
            text-align: center;
            border-radius: 5px;
            margin-bottom: 10px;
        }
        .header div:not(:first-child){
            position: absolute;
        }
        .left{
            left:-50px;
        }
        .right{
            left:50px;
        }
    </style>
</head>
<body>
    <div class="header">
        <div class="active up ArrowUp">上</div>
        <div class="active down ArrowDown">下</div>
        <div class="active left ArrowLeft">左</div>
        <div class="active right ArrowRight">右</div>
    </div>
</body>
</html>
<script>
    const div = document.querySelectorAll('.header .active')
    // console.log(div);
    document.addEventListener('keydown',e => {
        // div.forEach(li => {
        //     if(li.classList.contains(e.key)){
        //         li.style.background = 'red'
        //     }
        // })
        document.querySelector(`.${e.key}`).style.background = 'red'
    })
    document.addEventListener('keyup',f)
    function f(li){
        div.forEach(li => {
        li.style.background = 'none'
    })
    }
</script>